package com.binc.testspring.guava;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * FileName: MultiMapTest
 * Autho: binC
 * Date:  2022/1/18 15:31
 */
public class MultiMapTest {

    public static void main(String... args) {
        Multimap<String, String> myMultimap = ArrayListMultimap.create();



        // Adding some key/value
        myMultimap.put("Fruits", "Bannana");
        myMultimap.put("Fruits", "Apple");
        myMultimap.put("Fruits", "Pear");
        myMultimap.put("Fruits", "Pear");
        myMultimap.put("Vegetables", "Carrot");
        myMultimap.put("binc", "binccccc");

        // Getting the size
        int size = myMultimap.size();
        System.out.println(size); // 5    注意这个地方 是有点不太好玩的，如果能返回2的话，使用场景可能更多一些；   应该有别的解决办法（如果获取key集合之类的，再去个重）  下边这两个方法就很合适



        System.out.println("---------------");
        Map<String, Collection<String>> stringCollectionMap = myMultimap.asMap();
        System.out.println(stringCollectionMap); //{binc=[binccccc], Vegetables=[Carrot], Fruits=[Bannana, Apple, Pear, Pear]}

        System.out.println("1111111111111");
        Set<String> keySet = myMultimap.keySet();
        System.out.println(keySet); //[binc, Vegetables, Fruits]

//////////////////////
        // Getting values
        Collection<String> fruits = myMultimap.get("Fruits");
        System.out.println(fruits); //  [Bannana, Apple, Pear, Pear]
        System.out.println(ImmutableSet.copyOf(fruits));// [Bannana, Apple, Pear]
        // Set<Foo> set = Sets.newHashSet(list);
        // Set<Foo> foo = new HashSet<Foo>(myList);

        Collection<String> vegetables = myMultimap.get("Vegetables");
        System.out.println(vegetables); // [Carrot]

        // Iterating over entire Mutlimap
        for (String value : myMultimap.values()) {
            System.out.println(value);
        }

        // Removing a single value
        myMultimap.remove("Fruits", "Pear");
        System.out.println(myMultimap.get("Fruits")); // [Bannana, Apple, Pear]

        // Remove all values for a key
        myMultimap.removeAll("Fruits");
        System.out.println(myMultimap.get("Fruits")); // [] (Empty Collection!)



    }
}
